package day19;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class Day19 {
    public static void main(String[] args) throws InterruptedException {

         class MyThread extends Thread{
            @Override
            public void run() {
                for (int i = 1;i<=100;i++){
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println();
                }
            }
        }

        Thread thread1 = new MyThread();
//1、继承Thread类，重写run方法
         class MyRunnable implements Runnable{
             @Override
             public void run() {
                 for (int i = 1;i<=100;i++){
                     try {
                         Thread.sleep(20);
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                     }
                     System.out.println();
                 }
             }
         }
        Runnable runnable = new MyRunnable();
         Thread thread2 = new Thread(runnable);
//2、实现Runnable接口,重写run方法

        class MyCallable implements Callable<Integer>{
            @Override
            public Integer call() throws Exception {
                int i;
                for ( i = 1;i<=100;i++){
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println();
                }
                return i;
            }
        }
        Callable callable = new MyCallable();
        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        Thread thread3 = new Thread(futureTask);
//3、使用Callable和FutureTask创建线程
//4、用法
        thread1.start();
        thread1.join();
//Thread.yield();
        thread1.setDaemon(true);
        thread1.setPriority(8);

    }

//2、线程的五种状态
//1.使用new创建一个线程，使线程处于new（新建）状态；
//2.调用start();方法，使线程处于Runnable(可执行)状态；
//3、由操作系统的任务调度机制使线程处于Running(运行)状态；
//4、线程正常结束或异常退出后处于Dead(死亡)状态;
//5、sleep()与jion()方法分别会使当前线程与当前线程所在线程进入Blocked(阻塞)状态;
//sleep()与jion()方法执行结束后会分别使当前线程与当前线程所在线程回到Runnable(可执行)状态,继续执行;


//4、乐观锁的原理
//CAS乐观锁机制中使用了3个基本操作数:内存地址，旧的预期值，要修改的新值
//当线程进行操作时，如果内存地址与旧的预期值相同，则变成要修改的新值
//如果内存地址与旧的预期值不同，则重新获取内存地址与旧的预期值，相等的时候在执行;
}